var utils = require('../utils/collide_tools');
var Shares = require('gameshare');
var GameObject = Shares.GameObject;
var GameEvent = Shares.GameEvent;
var GameConst = Shares.GameConst;
var SceneTypes = Shares.SceneTypes;
cc.Class({
    extends: cc.Component,

    properties: {
        direction: 0,
        inRotate: false,
        screenPos: cc.v2(0,0),
        objectType: {
            type:GameObject,
            default: GameObject.StaticTri
        },
        isUnActive: false,
    },

    // use this for initialization
    onLoad: function () {
        this.size = GameConst.halfsize,
        cc.systemEvent.on(GameEvent.GameTouched, this.onGameTouched, this);
    },

    onDestroy () {
        cc.systemEvent.off(GameEvent.GameTouched, this.onGameTouched, this);
    },
    
    start: function () {
        var self = this;
        var size = this.size;
        this.posOff = [];
        this.posOff[0] = [cc.v2(-size, -size), cc.v2(-size, size), cc.v2(size, -size)]; //0
        this.posOff[1] = [cc.v2(-size, size), cc.v2(size, size), cc.v2(-size, -size)];  //90
        this.posOff[2] = [cc.v2(size, size), cc.v2(size, -size), cc.v2(-size, size)];   //180
        this.posOff[3] = [cc.v2(size, -size), cc.v2(-size, -size), cc.v2(size, size)];  //270
        //this.drawDebug();
        this.alineToLines();
    },

    onGameTouched: function(){
        if(this.objectType == GameObject.RotateTri){
            this.rotateRight();
        }
        else if(this.objectType == GameObject.SwitchTri){
            this.switchShow();
        }

        if(cc.zsb.sceneType == SceneTypes.SceneEdit){
            if(this.objectType == GameObject.StaticTri){
                this.rotateRight();
            }
        }
            
    },

    setData: function(data){
        this.objectType = data.objectType;
        this.direction = data.direction;
        this.node.position = data.position;
        this.isUnActive = data.isUnActive;
        this.refresh();
    },
    
    toData: function(){
        var data = {
            objectType: this.objectType,
            direction: this.direction,
            position: this.node.position,
            isUnActive: this.isUnActive,
        }
        console.log(data)
        return data;
    },

    switchShow: function(){
        this.isUnActive = !this.isUnActive;
        this.refresh();
    },

    refresh: function(){
        if(this.isUnActive){
            this.node.opacity = 80;
        }
        else{
            this.node.opacity = 255;
        }

        this.node.rotation = this.direction * 90;

        if(this.objectType == GameObject.RotateTri)
        {
            this.node.children[3].active = true;
            this.node.children[2].active = false;
            this.node.children[4].active = false;
        }
        else if(this.objectType == GameObject.StaticTri)
        {
            this.node.children[3].active = false;
            this.node.children[2].active = true;
            this.node.children[4].active = false;
        }
        else if(this.objectType == GameObject.SwitchTri)
        {
            this.node.children[3].active = false;
            this.node.children[2].active = false;
            this.node.children[4].active = true;
        }
    },

    alineToLines: function(){
        var x = this.node.x - this.size;
        x = Math.floor(x/this.size/2)*(this.size * 2) + this.size;
        var y = this.node.y - this.size;
        y = Math.floor(y/this.size/2)*(this.size * 2) + this.size;
        this.node.x = x;
        this.node.y = y;
    },

    // called every frame
    update: function (dt) {
        if (cc.zsb && cc.zsb.player) {
            var player = cc.zsb.player;
            if (player && player.isPause == false && this.isUnActive == false) {
                var collide = false;
                this.screenPos = this.node.convertToWorldSpaceAR(cc.v2(0, 0));
                var playerScreenPos = player.getScreenPos();
                if (this.isInRect(playerScreenPos)) {
                    collide = utils.checkCollisionWithTri(player, this);
                }

                if (collide) {
                    console.log('collide');
                    var playerDir = this.getDirAfterCollide(player.dir);
                    player.doAfterCollide(this.node, playerDir);
                }
            }
        }
    },

    getDirAfterCollide: function(playerdir){
        if(this.direction == 0 && playerdir.equals(cc.v2(1, 0))){
            return cc.v2(-1, 0);
        }
        else if(this.direction == 0 && playerdir.equals(cc.v2(-1, 0))){
            return cc.v2(0, 1);
        }
        else if(this.direction == 0 && playerdir.equals(cc.v2(0, 1))){
            return cc.v2(0, -1);
        }
        else if(this.direction == 0 && playerdir.equals(cc.v2(0, -1))){
            return cc.v2(1, 0);
        }

        if(this.direction == 1 && playerdir.equals(cc.v2(1, 0))){
            return cc.v2(-1, 0);
        }
        else if(this.direction == 1 && playerdir.equals(cc.v2(-1, 0))){
            return cc.v2(0, -1);
        }
        else if(this.direction == 1 && playerdir.equals(cc.v2(0, 1))){
            return cc.v2(1, 0);
        }
        else if(this.direction == 1 && playerdir.equals(cc.v2(0, -1))){
            return cc.v2(0, 1);
        }
        console.log(playerdir);
        if(this.direction == 2 && playerdir.equals(cc.v2(1, 0))){
            return cc.v2(0, -1);
        }
        else if(this.direction == 2 && playerdir.equals(cc.v2(-1, 0))){
            return cc.v2(1, 0);
        }
        else if(this.direction == 2 && playerdir.equals(cc.v2(0, 1))){
            return cc.v2(-1, 0);
        }
        else if(this.direction == 2 && playerdir.equals(cc.v2(0, -1))){
            return cc.v2(0, 1);
        }

        if(this.direction == 3 && playerdir.equals(cc.v2(1, 0))){
            return cc.v2(0, 1);
        }
        else if(this.direction == 3 && playerdir.equals(cc.v2(-1, 0))){
            return cc.v2(1, 0);
        }
        else if(this.direction == 3 && playerdir.equals(cc.v2(0, 1))){
            return cc.v2(0, -1);
        }
        else if(this.direction == 3 && playerdir.equals(cc.v2(0, -1))){
            return cc.v2(-1, 0);
        }
    },

    isInRect: function (pos) {
        var posscreen = this.screenPos;
        if (pos.x < posscreen.x + this.size &&
            pos.x > posscreen.x - this.size &&
            pos.y < posscreen.y + this.size &&
            pos.y > posscreen.y - this.size) {
            return true;
        }

        return false
    },

    rotateRight: function () {
        if (this.inRotate == true) {
            return
        }
        this.inRotate = true;
        this.direction = (this.direction + 1) % 4
        var action = cc.rotateBy(0.1, 90);
        this.node.runAction(action);
        setTimeout((function () {
            this.inRotate = false;
        }).bind(this), 200);
        console.log(this.node.rotation)
        console.log(this.direction)
    },

    drawDebug: function () {
        var size = this.size;
        var ctx = this.node.getChildByName('debugdraw').getComponent(cc.Graphics);
        ctx.moveTo(-size, -size);
        ctx.lineTo(-size, size);
        ctx.lineTo(size, -size);
        ctx.close();
        ctx.stroke();
        ctx.fill();
    },

    getScreenPos: function () {
        var poso = this.screenPos;
        var posa = poso.add(this.posOff[this.direction][0]);
        var posb = poso.add(this.posOff[this.direction][1]);
        var posc = poso.add(this.posOff[this.direction][2]);
   
        return [posa, posb, posc];
    }
});